package com.moleq.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.moleq.base.MoleQBaseDialog;
import com.moleq.base.OnResultListener;
import com.moleq.device.DeviceManager;
import com.moleq.mgpos.activities.R;

public class MessageDialog extends Dialog {

	public static final int ICON_INFO = 0;
	public static final int ICON_ERROR = 1;

	public static final int MESSAGE_BOX_OK = 0;
	public static final int MESSAGE_BOX_YES_NO = 1;

	private Button mButtonOK;
	private Button mButtonYes;
	private Button mButtonNo;
	private TextView mTextViewMsg;
	private ImageView mImageView;
	public int mStyle = 0;
	private Context mContext;
	private OnClickMessageListener mOnClickMessageListener;

	public void setStyle(int style) {
		this.mStyle = style;
		switch (style) {
		case MESSAGE_BOX_OK:
			mButtonOK.setVisibility(View.VISIBLE);
			mButtonYes.setVisibility(View.INVISIBLE);
			mButtonNo.setVisibility(View.INVISIBLE);
			break;
		case MESSAGE_BOX_YES_NO:
			mButtonOK.setVisibility(View.INVISIBLE);
			mButtonYes.setVisibility(View.VISIBLE);
			mButtonNo.setVisibility(View.VISIBLE);
			break;
		}

	}

	public void setMessageIcon(int icon) {
		switch (icon) {
		case ICON_INFO:
			mImageView.setImageResource(android.R.drawable.ic_dialog_info);
			break;
		case ICON_ERROR:
			mImageView.setImageResource(android.R.drawable.ic_delete);
			break;
		}
	}


	private Button.OnClickListener mOnClickListener = new Button.OnClickListener() {

		@Override
		public void onClick(View v) {
			
			
			switch (v.getId()) {
			case R.id.buttonMessageDialogOK:
				mOnClickMessageListener.onClickOK();
				dismiss();
				break;
			case R.id.buttonMessageDialogYes:
				mOnClickMessageListener.onClickYes();
				dismiss();
				break;
			case R.id.buttonMessageDialogNo:
				mOnClickMessageListener.onClickNo();
				dismiss();
				break;
			}

		}
	};


	public MessageDialog(Context context) {
		 super(context,R.style.Transparent_dialog);
		constructor(context);
	}

	public MessageDialog(Context context, int theme) {
		super(context, theme);
		constructor(context);
	}
	

	private void constructor(Context context) {
		mContext = context;
		
		this.setContentView(R.layout.dialog_message_box);
		this.setCancelable(false);

		mImageView = (ImageView) findViewById(R.id.imageViewMessgeDialogIcon);
		mTextViewMsg = (TextView) findViewById(R.id.textViewMessageDialogMsg);

		mButtonOK = (Button) findViewById(R.id.buttonMessageDialogOK);
//		mButtonOK.setTag(FunctionOption.NULL);
		mButtonOK.setOnClickListener(mOnClickListener);

		mButtonYes = (Button) findViewById(R.id.buttonMessageDialogYes);
//		mButtonYes.setTag(FunctionOption.NULL);
		mButtonYes.setOnClickListener(mOnClickListener);

		mButtonNo = (Button) findViewById(R.id.buttonMessageDialogNo);
//		mButtonNo.setTag(FunctionOption.NULL);
		mButtonNo.setOnClickListener(mOnClickListener);

		setMessageIcon(ICON_INFO);
		setStyle(MESSAGE_BOX_OK);
		setMessage("None");
	
		
	}
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		
	}


	public void setMessage(String msg) {
		mTextViewMsg.setText(msg);
	}

	
	public void setOnClickMessageListener (OnClickMessageListener listener){
		mOnClickMessageListener = listener;
	}
	
	public interface OnClickMessageListener{
		public void onClickYes();
		public void onClickNo();
		public void onClickOK();
		
		
	}
	
	public void show(){
		super.show();
		
		if (mContext instanceof OnResultListener) {
			OnResultListener linstener = (OnResultListener) this.mContext;
			linstener.onShowingMessageDlg(true);
		}
		
		DeviceManager.getInstance().disableAllScanner();
	}
	
	public void dismiss(){
		super.dismiss();
		
		if (mContext instanceof OnResultListener) {
			OnResultListener linstener = (OnResultListener) this.mContext;
			linstener.onShowingMessageDlg(false);
		}
		DeviceManager.getInstance().enableAllScanner();
	}

}
